
<h2 style='text-align:center'>成员权限管理</h2>

<div style='margin-bottom:10px;font-size:13px;text-align:center'>当前用户：<span id='MainAdminUserPermCurrentUser'></span>　<button onclick='MainAdminUserPerm_ResetUser()'>重置密码</button></div>

<div style='margin:20px auto'>
	权限状态:
	<select id="cityPermStat">
		<option value="1">全部城市</option>
		<option value="2">已开权限通城市</option>
		<option value="3">未开权限通城市</option>
	</select>
</div>

<div id='MainAdminUserPerm_AddPermArea' >
	<div id='LoginCityArea' style=''>
		<select id="PermMainCounty_Province"></select>
		<select id="PermMainCounty_City"></select>
		<select id="PermMainCounty_County"></select>
		<button id='MainAdminUserPerm_AddBtn'>添加</button>
	</div>
	<div>
	</div>
</div>

<div style='text-align:center;margin:20px 0'><button onclick='MainAdminUserPerm_Back()'>返回</button></div>


<script>


	//显示当前用户名
	$('#MainAdminUserPermCurrentUser').text(sessionStorage.admin_MainAdminUser_ControlUser);


	//状态维持与切换
	if(sessionStorage.admin_MainAdminUserPerm_PermStat){
		$('#cityPermStat').val(sessionStorage.admin_MainAdminUserPerm_PermStat);
	}
	$('#cityPermStat').change(function(){
		sessionStorage.admin_MainAdminUserPerm_PermStat = _getPermStat();
		_viewPermData();	//权限城市操作集合
	});
	
	_viewPermData();	//权限城市操作集合


	//添加或删除权限
	$('#MainAdminUserPerm_AddBtn').click(function(){
		if(!confirm('确认操作?')){
			return;
		}
		//获取县级信息 
		var PermMainCountry_CountVal = PermMainCounty_County.options[PermMainCounty_County.selectedIndex].value;
		var PermCityCode = PermMainCountry_CountVal.split(',')[0];
		if(!PermCityCode){
			A('缺少城市代码！');return;
		}

		var data = {
			type:'adminUserPermAdd',
			userId:sessionStorage.admin_MainAdminUser_ControlId,
			pcityCode:PermCityCode,
			permStat:_getPermStat()
		};
		AjaxAdmin(data,function(res){
			var tmp = _checkJsonFormat(res);
			if(tmp.stat == 'ok'){
				A('操作成功！');
				_viewPermData();	//权限城市操作集合
			}else if(tmp.stat == 'err2'){
				A('已有该城市权限!');
			}else if(tmp.stat == 'err3'){
				A('操作失败！');
			}
		});
	});

	//权限城市操作集合
	function _viewPermData(){
		//获取城市列表数据


		getCityInfoFull(function(cityList){
			//获取管理员的权限列表
			_getAdminUserPerm(function(permList){
				//解析城市到下拉框
				City_ParseCityForPerm(cityList,_getPermStat(),PermMainCounty_Province,PermMainCounty_City,PermMainCounty_County,function(){
				},permList);
			});
		});
	}





		/*
			### 解析城市数据到元素
			参一：object         城市全部数据对象
			参二：object         显示类型：1 全部，2 开通权限的 ，3 未开通权限的
			参三：int            返回城市数据类型，1：全部，2：已开通，3：未开通
			参四：object 	     省select对象
			参五：object 	     市select对象
			参六：object 	     县select对象
			参七：function       完成后的回调
			参八：parmList		 管理员权限列表(数组，用于是否筛选管理员权限的城市)
			示例：

		*/
		function City_ParseCityForPerm(CityInfo,stat,ele_province,ele_city,ele_county,fun,parmList){
			ele_county.innerHTML = '';
			ele_province.innerHTML='';
			ele_city.innerHTML = '';

			var Province_Arr = [];
			var City_Arr = [];
			var County_Arr = [];	//用来装载省市县分类后的城市数据

			var Province_Arr2 = new Array();	
			var City_Arr2 = new Array();
			var County_Arr2 = new Array();	//用来装载筛选后的城市数据
			LoadCityInfo();
		  	function LoadCityInfo(){
		  		var City_ids = [];
				for(var i=0;i<CityInfo.length;i++){
					//过滤掉港奥台
					if(CityInfo[i].level == 0){
						if(CityInfo[i].code == 710000 || CityInfo[i].code == 810000 || CityInfo[i].code == 820000){
							continue;
						}
					}
					if(CityInfo[i].level == 0){			//归集省数据
						Province_Arr2.push(CityInfo[i]);
					}
					if(CityInfo[i].level == 1){			//归集市数据
						City_Arr2.push(CityInfo[i]);
					}
					if(CityInfo[i].level == 2){			//归集县数据
					
						//筛选出已开通的城市
						if(CityInfo[i].db_host && CityInfo[i].db_name){
							//如果在权限列表中
							if(stat != 1){	//显示全部
								if(parmList.indexOf(parseInt(CityInfo[i].code)) >= 0){	//在权限列表中
									if(stat == 3){	//选择未开通时跳过
										continue;
									}
								}else{
									if(stat == 2){	//选择开通的
										continue;
									}
								}
							}
							County_Arr.push(CityInfo[i]);
							if(City_ids.indexOf(parseInt(CityInfo[i].parent_id)) < 0){
								City_ids.push(parseInt(CityInfo[i].parent_id));
							}
						}
					
						
					}
				}

				//筛选市数据
				var province_ids = [];
				for(var i=0;i<City_Arr2.length;i++){
					for(var j=0;j<City_ids.length;j++){
						if(parseInt(City_ids[j]) == parseInt(City_Arr2[i].id)){
							City_Arr.push(City_Arr2[i]);
							if(province_ids.indexOf(parseInt(City_Arr2[i].parent_id)) < 0){
								province_ids.push(parseInt(City_Arr2[i].parent_id));
							}
						}
					}
				}


				//筛选省数据
				for(var i=0;i<Province_Arr2.length;i++){
					for(var j=0;j<province_ids.length;j++){
						if(parseInt(province_ids[j]) == parseInt(Province_Arr2[i].id)){
							Province_Arr.push(Province_Arr2[i]);
						}
					}
				}

				PushProvince(); //填充省市县数据
		  	}

		 	//填充省数据
			function PushProvince(){
				ele_province.innerHTML = ''
				var Province_Str = '';
				for(var i=0;i<Province_Arr.length;i++){
					Province_Str += "<option value="+Province_Arr[i].id+">"+Province_Arr[i].name+"</option>";
				}
				ele_province.innerHTML=Province_Str;
				PushCity();		//直充市数据
			}
			//填充市数据
			function PushCity(){
				ele_city.innerHTML = '';
				var Province_Id = ele_province.options[ele_province.selectedIndex].value;
				var City_Str = '';
				for(var i=0;i<City_Arr.length;i++){
					if(City_Arr[i].parent_id == Province_Id){
						City_Str += "<option value="+City_Arr[i].id+">"+City_Arr[i].name+"</option>";
					}
				}
				ele_city.innerHTML = City_Str;
				PushCounty();	//填充县数据
			}
			//填充县数据
			function PushCounty(){
				ele_county.innerHTML = '';
				var City_Id = ele_city.options[ele_city.selectedIndex].value;
				var County_Str = '';
				for(var i=0;i<County_Arr.length;i++){
					if(County_Arr[i].parent_id == City_Id){
						County_Str += "<option value="+County_Arr[i].code+","+County_Arr[i].db_host+","+County_Arr[i].db_name+">"+County_Arr[i].name+"</option>";
					}
				}
				ele_county.innerHTML = County_Str;
				if(fun){
					fun();
				}
			}
			//省的切换事件
			ele_province.onchange = function(){
				PushCity(); 	//直充市数据
			}
			//市的切换事件
			ele_city.onchange = function(){
				PushCounty(); 	//直充市数据
			}
			//县的切换
			ele_county.onchange = function(){
				if(fun){
					fun();
				}
			}
		}






	//获取管理员的权限列表
	function _getAdminUserPerm(fun){
		var data = {
			type:'getAdminUserPer',
			user_id:sessionStorage.admin_MainAdminUser_ControlId
		}
		AjaxAdmin(data,function(res){
			var tmp = _checkJsonFormat(res);
			fun(tmp.data);
		});
	}


	//获取当前选择的权限类别
	function _getPermStat(){
		var pstat = $('#cityPermStat').find('option:selected').val();
		switch(pstat){
			case '1':$('#MainAdminUserPerm_AddBtn').hide();break;
			case '2':$('#MainAdminUserPerm_AddBtn').text('删除').show();break;
			case '3':$('#MainAdminUserPerm_AddBtn').text('添加').show();break;
		}
		return pstat;
	}





	//返回按钮
	function MainAdminUserPerm_Back(){
		$('#Main_AdminUser_Area2').hide();
		$('#Main_AdminUser_Area1').show();

		//初始化权限分类为全部城市(避免显示异常)
		sessionStorage.admin_MainAdminUserPerm_PermStat = 1;
	}





	//重置用户密码
	function MainAdminUserPerm_ResetUser(){
		if(!_confirm()){
			return;
		}
		var pwd = Math.round(Math.random(10)*1000);
		var data = {
			type:'resetAdminUserPwd',
			id:sessionStorage.admin_MainAdminUser_ControlId,
			pwd:pwd
		};
		AjaxAdmin(data,function(res){
			var tmp = _checkJsonFormat(res);
			if(tmp.stat == 'ok'){
				A('密码已重置为：'+pwd);
			}else{
				A('操作失败！');
			}
		});
	}







</script>